package com.pengcheng.hnweb.util;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Component
public class FileUtil {
    @Value("${msgpath}")
    private String msgpath;
    @Autowired
    private Common common;
    public boolean write(String filepath,String content)
    {
        try {
            File file=new File(filepath);
            File path=new File(file.getParent());
            if(!path.exists())
            {
                path.mkdirs();
            }
            if(!file.exists())
            {
                file.createNewFile();
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(filepath,StandardCharsets.UTF_8));
            writer.write(content);
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    public boolean create_file(String filepath)
    {
        try {
            File file=new File(filepath);
            File path=new File(file.getParent());
            if(!path.exists())
            {
                path.mkdirs();
            }
            if(!file.exists())
            {
                file.createNewFile();
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    public void append(String filepath,String content)
    {
        try {
            File file=new File(filepath);
            File path=new File(file.getParent());
            if(!path.exists())
            {
                path.mkdirs();
            }
            if(!file.exists())
            {
                file.createNewFile();
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(filepath,StandardCharsets.UTF_8,true));
            writer.write(content);
            writer.newLine();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String read(String filepath)
    {
        try {
            File file=new File(filepath);
            if(!file.exists())
            {
                return "";
            }
            return FileUtils.readFileToString(file,"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
    public void add_msg(String filename,String msg)
    {
        String now_date=common.format_date(new Date()).substring(0,10);
        String file_path=System.getProperty("user.dir")+msgpath+now_date+"/"+filename+".txt";
        append(file_path,common.format_date(new Date()));
        append(file_path,msg);
    }
    public void copy_file(String path1,String path2)
    {
        String sourcePath = path1;
        String targetPath = path2;
        File sourceFile = new File(sourcePath);
        File targetFile = new File(targetPath);
        try{
            File path=new File(targetFile.getParent());
            if(!path.exists())
            {
                path.mkdirs();
            }
            if(!targetFile.exists())
            {
                targetFile.createNewFile();
            }
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
            FileChannel sourceChannel = fileInputStream.getChannel();
            FileChannel targetChannel = fileOutputStream.getChannel();
            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            sourceChannel.close();
            targetChannel.close();
            fileInputStream.close();
            fileOutputStream.close();
            sourceFile.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public List<Map<String,Object>> list_file(File directory)
    {
        try {
            List<Map<String,Object>> fileList = new ArrayList<>();
            File[] files = directory.listFiles();
            for(int i=0;i<files.length;i++)
            {
                File f=files[i];
                Map<String,Object> fileinfo=new HashMap<>();
                fileinfo.put("name",f.getName());
                fileinfo.put("path",f.getAbsolutePath());
                fileinfo.put("size",f.length());
                fileinfo.put("is_file",f.isFile());
                fileinfo.put("modifydate",f.lastModified());
                fileList.add(fileinfo);
            }
            return fileList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
